package binarysearchTree;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-11-13
 * Time:15:55
 */
class BinarySearchTree{
    static class TreeNode{
        public  int val;
        public  TreeNode left;
        public  TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }

    public  TreeNode root;

    //查找结点
    public  TreeNode findVal(int val){
        TreeNode cur = root;
        while (cur != null){
            if(cur.val < val){
                cur = cur.right;
            }else if(cur.val == val){
                return  cur;
            }else{
                cur = cur.left;
            }
        }
        return  null;
    }

    //插入结点
    public  void insert(int val){
        TreeNode treeNode = new TreeNode(val);
        TreeNode cur = root;
        TreeNode parent = null;
        if(root == null){
            root = treeNode;
            return;
        }
        while(cur != null){
            if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == val){
                return;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if(parent.val > val){
            parent.left = treeNode;
        }else{
            parent.right = treeNode;
        }
    }

    public void remove(int key){
        //有没有这个结点
        //这个结点的父节点
        //父节点往下联接哪个结点
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key){
               //找到了要删除的结点
                //parent记录的是他的上一个结点
                removeNode(parent,cur);
                return;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
    }
    /*
    parent 要删除结点的父节点
    cur    要删除结点
     */
    private   void removeNode(TreeNode parent,TreeNode cur){
        //左树为空：删除根节点 删除根节点的左节点 删除根节点的右节点
        //右树为空：删除根节点  删除根节点的左节点  删除根节点的右节点
        //左树右树都不为空：
        // 替罪羊法  讲待删除的结点替换为当前结点的左子树的最大值或者当前结点的右树右子树的最小值
        //删除替罪羊
        if(cur.left == null){
            if(cur == root){
                root = root.right;
            } else if(cur == parent.left){
                parent.left = cur.right;
            } else{
                //此处表示的是：cur == parent.right
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = root.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{
                TreeNode tp = cur;
                TreeNode tar = cur.right;
                while (tar.left != null){
                    tp = tar;
                    tar = tar.left;
                }
                cur.val = tar.val;
                if(tar == tp.left){
                    tp .left = tar.right;
                }else{
                    tp.right = tar.right;
                }
        }
    }

}
public class Test {
    public static void main(String[] args) {
        BinarySearchTree bina  = new BinarySearchTree();
        int[] array = {12,3,6,10,8};
        for (int x :array) {
            bina.insert(x);
        }
        bina.remove(3);
        System.out.println("fafa");
    }
}
